package com.ecelin;

import com.ecelin.service.InfoHandler;
import com.ecelin.utils.CustomException;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author ecelin@ecelin.com
 */
public class MainApplication {
	public static void main(String[] args) {
		try {
			File file = new File(System.getProperty("user.dir"), "setting.properties");
			InputStream is;
			if (file.exists()) {
				is = new FileInputStream(file);
			} else {
				is = MainApplication.class.getClassLoader().getResourceAsStream("setting.properties");
			}
			Properties properties = new Properties();
			properties.load(is);
			if (is != null) {
				is.close();
			}
			String comma = ",";
			String tableName = properties.getProperty("tableName");
			if (tableName == null || "".equals(tableName.trim())) {
				throw new CustomException("tableName参数不能为空!");
			}
			if (tableName.contains(comma)) {
				String[] tableNameArray = tableName.trim().split(comma);
				for (String table : tableNameArray) {
					InfoHandler.collect(properties, table);
				}
			} else {
				tableName = tableName.trim();
				InfoHandler.collect(properties, tableName);
			}
			System.out.println("执行完成,项目'" + properties.getProperty("projectName").trim() + "'已经生成在桌面上!");
		} catch (Exception e) {
			e.printStackTrace();
			StackTraceElement[] stackTrace = e.getStackTrace();
			StringBuilder sb = new StringBuilder(e.getMessage());
			sb.append("\r\n");
			for (StackTraceElement stackTraceElement : stackTrace) {
				sb.append(stackTraceElement).append("\r\n");
			}
			throw new CustomException(sb.toString());
		}
	}
}
